package com.hangxintong.common.base.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hangxintong.common.base.entity.LmCompanyAppAssoc;
import com.hangxintong.common.base.mapper.CompanyAppAssocMapper;
import com.hangxintong.common.exception.SystemErrorCodeMark;
import com.hangxintong.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @Auther: tsj
 * @Date: 2021/3/11 09:50
 * @Description:
 */
@Service
public class LmCompanyAppAssocService extends ServiceImpl<CompanyAppAssocMapper, LmCompanyAppAssoc> {


    @Resource
    CompanyAppAssocMapper companyAppAssocMapper;

    @Override
    public CompanyAppAssocMapper getBaseMapper() {
        return companyAppAssocMapper;
    }

    /**
     * 判断企业有无权限执行方法
     *
     * @param companyId
     * @param xid
     * @return
     */
    public Result isCompanyAppAssocResult(Long companyId, String xid) {


        if (companyId == null || StringUtils.isEmpty(xid)) {
            return new Result(false, SystemErrorCodeMark.Code.USER_NO_PERMISSION.getErrorCode(), "无权限操作");
        }
        QueryWrapper<LmCompanyAppAssoc> queryWrapper = new QueryWrapper();

        queryWrapper.and(q -> q.eq("assoc_id", xid).eq("company_id", companyId).eq("status",2));

        LmCompanyAppAssoc lmCompanyAppAssoc = companyAppAssocMapper.selectOne(queryWrapper);

        if (lmCompanyAppAssoc == null) {
            return new Result(false, SystemErrorCodeMark.Code.USER_NO_PERMISSION.getErrorCode(), "无权限操作");
        }

        if (lmCompanyAppAssoc.getCompanyStatus() == 2) {
            return new Result(false, SystemErrorCodeMark.Code.USER_NO_PERMISSION.getErrorCode(), "无权限操作");
        }

        return Result.ok();
    }

    /**
     * 判断企业有无权限执行方法
     *
     * @param companyId
     * @param xid
     * @return
     */
    public boolean isCompanyAppAssocBoolean(Long companyId, String xid) {


        if (companyId == null || StringUtils.isEmpty(xid)) {
            return false;
        }
        QueryWrapper<LmCompanyAppAssoc> queryWrapper = new QueryWrapper();

        queryWrapper.and(q -> q.eq("assoc_id", xid).eq("company_id", companyId).eq("status",2));

        LmCompanyAppAssoc lmCompanyAppAssoc = companyAppAssocMapper.selectOne(queryWrapper);

        if (lmCompanyAppAssoc == null) {
            return false;
        }

        if (lmCompanyAppAssoc.getCompanyStatus() == 2) {
            return false;
        }

        return true;
    }


}
